package com.cpc.mback.controller;


import com.alibaba.fastjson.JSONObject;
import com.cpc.mback.RespEntity;
import com.cpc.mback.entity.Content;
import com.cpc.mback.entity.Record;
import com.cpc.mback.entity.User;
import com.cpc.mback.service.MainService;
import com.cpc.mback.service.RecordService;
import com.cpc.mback.service.UploadService;
import com.cpc.mback.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

@RestController
@Api(value = "社区接口", tags = "社区接口", description = "没有description")
@RequestMapping("/api/community")
public class CommunityController {

    @Autowired
    MainService mainService;

    @Autowired
    UploadService uploadService;
    @Autowired
    RecordService recordService;
    @Autowired
    UserService userService;

    @ApiOperation(value = "1.主页（请求参数为字符串类型，0为笔记，1为问答）", notes = "该接口为备用接口")
    @GetMapping("/main")
    public ResponseEntity page(@RequestParam String type, @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size) {
        if (type.equals("1")) {
            Content.Type type2 = Content.Type.qa;
            return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getMain(type2, PageRequest.of(page - 1, size))).build());
        } else if (type.equals("0")) {
            Content.Type type2 = Content.Type.standard;
            return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getMain(type2, PageRequest.of(page - 1, size))).build());
        } else {
            return ResponseEntity.status(400).body(RespEntity.code(400).body("不存在该类型").build());
        }
    }

    @ApiOperation(value = "2.分类主页（请求参数为字符串类型，0为笔记，1为问答；" +
            "sort从0到8分别为inSchool,up,hotIndustry,internship,findJob,resource,fun," +
            "sci,other）", notes = "该接口为备用接口")
    @GetMapping("/sorts")
    public ResponseEntity page2(@RequestParam String type,@RequestParam String sort, @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size) {
        if (type.equals("1")) {
            Content.Type type2 = Content.Type.qa;
            if (sort.equals("0")){
                Content.Sort sort2 = Content.Sort.inSchool;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("1")){
                Content.Sort sort2 = Content.Sort.up;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("2")){
                Content.Sort sort2 = Content.Sort.hotIndustry;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("3")){
                Content.Sort sort2 = Content.Sort.internship;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("4")){
                Content.Sort sort2 = Content.Sort.findJob;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("5")){
                Content.Sort sort2 = Content.Sort.resource;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("6")){
                Content.Sort sort2 = Content.Sort.fun;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("7")){
                Content.Sort sort2 = Content.Sort.sci;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("8")){
                Content.Sort sort2 = Content.Sort.other;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
        }
        if (type.equals("0")) {
            Content.Type type2 = Content.Type.standard;
            if (sort.equals("0")){
                Content.Sort sort2 = Content.Sort.inSchool;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("1")){
                Content.Sort sort2 = Content.Sort.up;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("2")){
                Content.Sort sort2 = Content.Sort.hotIndustry;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("3")){
                Content.Sort sort2 = Content.Sort.internship;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("4")){
                Content.Sort sort2 = Content.Sort.findJob;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("5")){
                Content.Sort sort2 = Content.Sort.resource;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("6")){
                Content.Sort sort2 = Content.Sort.fun;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("7")){
                Content.Sort sort2 = Content.Sort.sci;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
            if (sort.equals("8")){
                Content.Sort sort2 = Content.Sort.other;
                return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getSort(type2,sort2 ,PageRequest.of(page - 1, size))).build());
            }
        }

            return ResponseEntity.status(400).body(RespEntity.code(400).body("不存在该类型").build());

    }


    @ApiOperation(value = "5.详情页", notes = "")
    @GetMapping("/get")
    public ResponseEntity getInfo(@RequestParam String id,@RequestParam String openId,@RequestParam String contentAuthorId) {
        /*
        if (userService.checkRegistered(payload.openId)){
            Content content = mainService.fetch(Long.valueOf(payload.id));
            recordService.createRecord(Long.valueOf(payload.openId),Long.valueOf(payload.id),"",0);
            return ResponseEntity.status(200).body(RespEntity.code(200).body(content).build());
        }else {
            userService.SignUp(payload.openId);
            Content content = mainService.fetch(Long.valueOf(payload.id));
            recordService.createRecord(Long.valueOf(payload.openId),Long.valueOf(payload.id),"",0);
            return ResponseEntity.status(200).body(RespEntity.code(200).body(content).build());
        }*/
        Content content = mainService.fetch(Long.valueOf(id));
        recordService.createRecord(openId,Long.valueOf(id),"",0,false,contentAuthorId);
        return ResponseEntity.status(200).body(RespEntity.code(200).body(content).build());
        //JSONObject result = new JSONObject();

    }

    @ApiOperation(value = "6.一级评论页", notes = "参数正确，可参照自动生成的格式")
    @GetMapping("/get-comments")
    public ResponseEntity getComments(@RequestParam String  id,@RequestParam String openId,@RequestParam String contentAuthorId, @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer size) {


        //return recordService.getRecord(Long.valueOf(id), openId,PageRequest.of(page - 1, size));
        return ResponseEntity.status(200).body(RespEntity.code(200).body(recordService.getRecord(Long.valueOf(id), openId,contentAuthorId,PageRequest.of(page - 1, size))).build());

    }

    @ApiOperation(value = "7.二级评论页", notes = "参数正确，可参照自动生成的格式")
    @GetMapping("/get-sub-comments")
    public ResponseEntity getSubComments(@RequestParam String  id,@RequestParam String openId, @RequestParam(defaultValue = "1") Integer page,
                                      @RequestParam(defaultValue = "10") Integer size) {


        //return recordService.getRecord(Long.valueOf(id), openId,PageRequest.of(page - 1, size));
        return ResponseEntity.status(200).body(RespEntity.code(200).body(recordService.getSubRecord(Long.valueOf(id), openId,PageRequest.of(page - 1, size))).build());

    }

    @ApiOperation(value = "1.关注的主页（请求参数为字符串类型，0为笔记，1为问答）", notes = "该接口为备用接口")
    @GetMapping("/follow")
    public ResponseEntity followPage(@RequestParam String type, @RequestParam(defaultValue = "1") Integer page,
                               @RequestParam(defaultValue = "10") Integer size) {
        if (type.equals("1")) {
            Content.Type type2 = Content.Type.qa;
            return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getMain(type2, PageRequest.of(page - 1, size))).build());
        } else if (type.equals("0")) {
            Content.Type type2 = Content.Type.standard;
            return ResponseEntity.status(200).body(RespEntity.code(200).body(mainService.getMain(type2, PageRequest.of(page - 1, size))).build());
        } else {
            return ResponseEntity.status(400).body(RespEntity.code(400).body("不存在该类型").build());
        }
    }


}
